class Solution {
public:
    string ans;int n,m;
    string s;
    void dfs(string now)
    {
        if(now.size()>ans.size()) ans=now;
        for(char ch='z';ch>='a';ch--)
        {
            now+=ch;
            int cnt=0;
            for(int i=0,j=0;i<n;i++)
            {
                if(s[i]==now[j])
                {
                    j++;
                    if(j>=now.size()) {j=0;cnt++;}
                }
            }
            if(cnt>=m) dfs(now);
            now.pop_back();
        }
    }
    string longestSubsequenceRepeatedK(string S, int K) {
    	n=S.size();m=K;ans="";s=S;
        dfs("");return ans;
    }
};